package com.itheima.user.controller;

import com.itheima.commonmodule.Result.Result;
import com.itheima.commonmodule.annotation.LogAsync;
import com.itheima.user.dto.CreateUserDto;
import com.itheima.user.dto.LoginDTO;
import com.itheima.user.dto.UpdateUserDto;
import com.itheima.user.dto.UserQueryDto;
import com.itheima.user.entity.UserRoleEntity;
import com.itheima.user.mapper.UserMapper;
import com.itheima.user.vo.LoginVO;
import com.itheima.user.vo.UserVo;
import com.mybatisflex.core.paginate.Page;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.itheima.user.services.IUserService;
import com.itheima.user.entity.UserEntity;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

/**
 * 用户表 控制层。
 *
 * @author mybatis-flex-helper automatic generation
 * @since 1.0
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private IUserService userService;
    @Autowired
    private UserMapper userMapper;

    /**
     * 添加 用户表
     *
     * @param user 用户表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PutMapping("/update")
    public Result update(@RequestBody @Valid UpdateUserDto user) {
        userService.updateUser(user);
        return Result.success(user);
    }
    @PostMapping("/create")
    public Result save(@RequestBody CreateUserDto user) {
        log.info("添加用户表：{}", user);
        return Result.success(userService.createUser(user));
    }

    /**
     * 根据主键删除用户表
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
//    @DeleteMapping("/remove/{id}")
//    public boolean remove(@PathVariable Serializable id) {
//        return userService.removeById(id);
//    }


    /**
     * 根据主键更新用户表
     *
     * @param user 用户表
     * @return {@code true} 更新成功，{@code false} 更新失败
     */

    @LogAsync
    @PostMapping("/login")
    public Result login(@RequestBody @Valid LoginDTO user) {
        LoginVO login = userService.login(user);
        return Result.success(login);
    }
    /**
     * 查询所有用户表
     *
     * @return 所有数据
     */
    @LogAsync
    @GetMapping("/list")
    public List<UserEntity> list() {
        return userService.list();
    }

    /**
     * 根据用户表主键获取详细信息。
     *
     * @param id user主键
     * @return 用户表详情
     */
    @GetMapping("/getInfo/{id}")
    public UserEntity getInfo(@PathVariable Serializable id) {
        return userService.getById(id);
    }


    /**
     * 分页查询用户表
     *
     * @param page 分页对象
     * @return 分页对象
     */
    //分页查询

    @GetMapping("/page")
    public Result page(@RequestBody UserQueryDto  page) {
        return Result.success(userService.pageList(page));
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        userService.deleteUser(id);
        return Result.success(id) ;
    }

}